/**
 * 查询条件生成函数
 */
import {
  ConditionUnit,
  SqlDataType
} from "../SqlUtil";


/**
 * 生成一个in条件
 *
 * @param key 属性名
 * sql:
 * values:
 */
export function getConditionUnitWithIn(key: string, arr: SqlDataType[]): ConditionUnit {
  const unit: ConditionUnit = {
    sql: "",
    values: []
  }
  if (arr.length) {
    let sql = `${key} IN (${getParamsTemplate(arr.length)})`
    unit.sql = sql;
    unit.values = arr;
  }
  return unit;
}


/**
 * 生成一个between条件
 *
 * @param key 属性名
 * sql:
 * values:
 */
export function betweenCondition(key: string, start: SqlDataType, end: SqlDataType): ConditionUnit {
  const unit: ConditionUnit = {
    sql: "",
    values: []
  }
  let sql = `${key} BETWEEN ? AND ?`
  unit.sql = sql;
  unit.values = [start, end];
  return unit;
}


/**
 * 描述一个 相等的 条件
 */
export function getConditionUnitWithEqual(key: string, value: SqlDataType): ConditionUnit {
  const unit: ConditionUnit = {
    sql: `${key} = ?`,
    values: [value]
  }
  return unit;
}

/**
 * 描述一个 相等的 条件
 */
export function getConditionUnitWithBelow(key: string, value: SqlDataType,includeEqual:boolean = false): ConditionUnit {
  const operator = "<";
  const unit: ConditionUnit = {
    sql: `${key} ${operator} ?`,
    values: [value]
  }
  return unit;
}

/**
 * 返回预编译参数字符串
 * eg: " ? , ? , ? "
 *
 * @param n
 * @returns
 */
export function getParamsTemplate(n: number = 1): string {
  const arr = [];
  for (let i = 0; i < n; i++) {
    arr.push("?");
  }
  const _str = arr.join(" , ");
  return ` ${_str} `;
}